package com.cjg.eightLock;

import java.util.concurrent.TimeUnit;

public class demo05 {
    public static void main(String[] args) throws InterruptedException {
        //多个对象只有一个Class 所i有还是先eat
        person2 person = new person2();
        person2 person1 = new person2();
        //synchronized 锁的是对象 ,两个线程锁的都是 person 所有eat先执行
        new Thread(()->{
            person.eat();
        }).start();
        TimeUnit.SECONDS.sleep(1);
        new Thread(()->{
            //锁对象不一样 eat睡 导致sleep先出现
            person.sleep();
        }).start(); new Thread(()->{
            //锁对象不一样 eat睡 导致sleep先出现
            person1.sleep();
        }).start();
    }
}
class person2{
    //加上 static 锁的是Class对象 所有先eat
    public static synchronized void eat(){

        //还是先执行 eat 但是sleep 沉睡 导致 run先出来
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("eat");
    }
    public static synchronized void sleep(){
        System.out.println("sleep");
    }
    public  void run(){
        System.out.println("run");
    }
}